Skip to content

#313- Add in-memory template caching to speed up PDF processing#314

Open
Cubix33 wants to merge 1 commit intofireform-core:mainfrom
Cubix33:template-caching
Open

#313- Add in-memory template caching to speed up PDF processing#314
Cubix33 wants to merge 1 commit intofireform-core:mainfrom
Cubix33:template-caching

Conversation

@Cubix33
Copy link

@Cubix33 Cubix33 commented Mar 21, 2026

Closes #313

📝 What does this PR do?

This PR significantly speeds up the PDF generation process by preventing the system from re-reading the same blank PDF form from the hard drive multiple times.

Instead of reading the template from scratch for every single request, the system now saves the raw PDF layout into the server's memory (RAM) the first time it sees it. Any future requests for that same form are loaded instantly from memory, making batch processing and repeated forms lightning fast!

🛠️ Changes Made

  • Template Caching (src/filler.py): Created a self._template_cache dictionary inside the Filler class to store the raw bytes of the PDF files.
  • Instant Loading (src/filler.py): Updated the fill_form function. If a form is new, it reads it from the disk and caches it. If it's already in the cache, it bypasses the disk entirely and loads instantly using PdfReader(fdata=...).
  • Test Script (src/main.py): Temporarily added a "double run" at the bottom of main.py to easily demonstrate the cache working in real-time.

✅ How to Test

  1. Run the main process (e.g., make exec or python src/main.py).
  2. Watch the terminal output. You will see the system run twice:
    • Run 1: Terminal prints [LOG] Template Cache Miss... (It reads the file from disk).
    • Run 2: Terminal prints [LOG] Template Cache Hit! (It successfully bypasses the disk and uses the fast memory).
  3. Verify that the final filled PDFs are generated correctly.

EXAMPLE SCREENSHOT:

image image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT]: Make PDF Processing Faster with Template Caching

1 participant